package com.etc.controller;

import com.etc.entity.OrderSku;
import com.etc.entity.Shoppingcart;
import com.etc.service.OrderSkuService;
import com.etc.service.impl.ShoppingcartServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/OrderSkuController")
public class OrderSkuController {
    @Autowired
    OrderSkuService orderSkuService;
    @Autowired
    ShoppingcartServiceImpl shoppingcartService;

    @RequestMapping("/show")
    @ResponseBody
    public PageInfo<OrderSku> show(Integer unm) {
        PageInfo<OrderSku> list = orderSkuService.getselectByPrimaryKey(unm);
        return list;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ModelAndView add(@RequestParam("id") Long id) {
        /*String id = request.getParameter("id");
        long idLong = Long.parseLong(id);*/
        ArrayList<OrderSku> list = new ArrayList<OrderSku>();
        List<Shoppingcart> shoppingcarts = shoppingcartService.doFindByUserId(id);
        for (Shoppingcart s : shoppingcarts) {
            list.add(new OrderSku(String.valueOf(s.getGoodsId()), s.getPrice(), s.getGoodsCount(), s.getImg()));
        }
        boolean b = orderSkuService.getinsert(list);
        if (b) {
            shoppingcartService.doDelA(id);
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/front/order.jsp");

        return mv;
    }

    @ResponseBody
    @RequestMapping("/rmv")
    public boolean rmv(String id) {
        boolean b = orderSkuService.getdeleteByPrimaryKey(id);
        return b;
    }

    @ResponseBody
    @RequestMapping("/sorr")
    public PageInfo<OrderSku> sorr(@RequestParam(value = "spc") String spc, @RequestParam(value = "unm") Integer unm) {
        PageInfo<OrderSku> list = orderSkuService.getselectByStasus(spc, unm);
        return list;
    }

    @RequestMapping("/rem")
    @ResponseBody
    public boolean rem(@RequestParam(value = "spc") String spc, @RequestParam(value = "n") String n) {
        boolean b = orderSkuService.upBystaus(spc, n);
        return b;
    }

    @RequestMapping("/shop")
    @ResponseBody
    public List<OrderSku> shop() {
        List<OrderSku> list = orderSkuService.getselectByall();
        return list;
    }

    @RequestMapping("/byid")
    @ResponseBody
    public OrderSku byid(@RequestParam(value = "id") String id) {
        OrderSku orderSku = orderSkuService.getselectById(id);
        return orderSku;
    }
}
